package org.shilong.UI.party;

import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;

import org.shilong.MContext.Json.JSonContext;
import org.shilong.MContext.Party.PartyContext;
import org.shilong.MView.PullToRefreshListView;
import org.shilong.Model.PartyModel;
import org.shilong.UI.R;
import org.shilong.Utility.PartyComparotr;
import org.shilong.Utility.SharedPreferenceUtility;
import org.shilong.Utility.ToastUtility;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MyPariesWhichITakePartInFrame extends Activity {

	private String[] arr;

	private PullToRefreshListView show_me_take_part_in_listview;

	/* 存放用户参加的聚会的list */
	public static LinkedList<PartyModel> pariesOfMeTakePartInList = new LinkedList<PartyModel>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.layout_i_take_part_in);

		arr = SharedPreferenceUtility.getPersonInfoFromSharedPreference( this );
		
		initViews();

		registerEvents();
	}

	private void initViews() {
		// TODO Auto-generated method stub

		show_me_take_part_in_listview = (PullToRefreshListView) this
				.findViewById(R.id.show_me_take_part_in_listview);

		show_me_take_part_in_listview.setAdapter(new
				MyAdapter(pariesOfMeTakePartInList));

	}

	private void registerEvents() {

		show_me_take_part_in_listview
		.setOnRefreshListener(new PullToRefreshListView.OnRefreshListener() {

			@Override
			public void onRefresh() {
				// TODO Auto-generated method stub


				GetDatas task = new GetDatas();

				task.execute();

			}
		});

	}

	/**
	 * 想list中加入新的数据
	 * 
	 * @param newList
	 */
	private void addDatasToList(LinkedList<PartyModel> newList) {

		pariesOfMeTakePartInList = new LinkedList<PartyModel>();

		Iterator<PartyModel> it = newList.iterator();

		while (it.hasNext()) {

			PartyModel partyModel = it.next();

			pariesOfMeTakePartInList.addFirst(partyModel);

		}

		Collections.sort(pariesOfMeTakePartInList, new PartyComparotr());

	}

	private class GetDatas extends AsyncTask<String, String, String> {

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub

			String rlt = "nodatas";

			rlt = (String) PartyContext.getInstance(
					PartyContext.QUERY_PARTIES_ME_TAKE_PART_IN ).getRlt(arr[2]);

			return rlt;
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub

			if( result.equals("{\"rlt\":[]}")){
				ToastUtility.showToast( MyPariesWhichITakePartInFrame.this, "暂无数据" );
			}else{

				System.out.println("新刷新参加的的聚会信息是:" + result);

				LinkedList<PartyModel> newll = new LinkedList<PartyModel>();

				newll = (LinkedList<PartyModel>) JSonContext.createInstance(
						JSonContext.TYPE_GET_PARIES_ME_TAKE_PART_IN_ME).getJsonRlt( result);


				addDatasToList(newll);

			}


			show_me_take_part_in_listview.onRefreshComplete();

			super.onPostExecute(result);
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			System.out.println("开始重新刷新我发起的聚会");
			super.onPreExecute();
		}

	}

	private class MyAdapter extends BaseAdapter {
		private LinkedList<PartyModel> mList;

		public MyAdapter(LinkedList<PartyModel> ll) {

			this.mList = ll;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mList.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return mList.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			// TODO Auto-generated method stub

			final int position = arg0;

			LayoutInflater inflater = (LayoutInflater) MyPariesWhichITakePartInFrame.this
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

			View root = inflater.inflate(R.layout.inner_listview_party_layout,
					null);

			LinearLayout layout = (LinearLayout) root
					.findViewById(R.id.inner_party_box_layout);

			if (arg0 % 2 != 0) {
				layout.setBackgroundResource(R.drawable.msgbox_rec);
			} else {
				layout.setBackgroundResource(R.drawable.msgbox_send);
			}

			TextView title = (TextView) root
					.findViewById(R.id.party_listview_title_textview);
			TextView time = (TextView) root
					.findViewById(R.id.party_listview_time_textview);
			TextView content = (TextView) root
					.findViewById(R.id.party_listview_content_textview);

			PartyModel model = new PartyModel();

			model = mList.get(arg0);

			System.out.println("聚会的id:" + model.getPa_seq_id());

			title.setText("标题:" + model.getPa_title());

			time.setText("时间:" + model.getPa_start_time() + "--"
					+ model.getEnd_time());
			content.setText("内容:" + model.getPa_comment());

			root.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {

					// quickActionMenu(v, mList.get(position));
				}
			});

			return root;

		}
	}

}
